require(['init'], function () {
/**
 * @author Levi
 * @url http://levi.cg.am/archives/3467
 */
    //获取课程
    var flage=true;
    var $EditAdd = $('<div class="formList Nformstyle" id="J_enroll_wrap" >' +
                         '<input type="hidden" name="coursetypeuid">' +
                     '<div class="formLi1 Snameinput">' +
                        '<div class="formTitle1 required">' +
                            '<label style="color:red">*</label><span>'+i18next.t('index.coursetype')+'：</span>' +
                        '</div>' +
                        '<div class="formContent1">' +
                            '<input class="form-control a1 focus studentname"  type="text" name="coursetypename"   data-i18n="[placeholder]input.entercourse" data-formcheck="length">' +
                      '</div>' +
                   ' </div>' +
                    '<div class="formLi1 Snameinput">' +
                        '<div class="formTitle1  ">' +
                            '<label style="color:red">*</label><span>'+i18next.t('table.coursecolor')+'：</span>' +
                        '</div>' +
                            '<div class="formContent1 Color" style="position:relative">' +
                            '<input class="form-control a1 focus"  type="text" placeholder=""  id="lineColor"  name="mycolor"  hx="fff"  >' +
                        '</div>' +
                   '</div>' +
                   '<div class="formLi1 Snameinput">' +
                       '<div class="formTitle1  ">' +
                           '<span>'+i18next.t('table.write')+'：</span>' +
                       '</div>' +
                           '<div class="formContent1 Color" style="position:relative">' +
                           '<input class="form-control a1 focus"   type="text"  placeholder=""  id="witercolor"  name="color">'+
                       '</div>'+
                  '</div>'+
                   '<div class="formLi1 Snameinput">' +
                        '<div class="formTitle1  ">' +
                            '<span>'+i18next.t('popupbox.remark')+'：</span>' +
                        '</div>' +
                        '<div class="formContent1">' +
                            '<input class="form-control a1 focus studentname"  type="text" name="remark" data-i18n="[placeholder]input.enterremark"  >' +
                        '</div>' +
    '</div>');
     function getCouerse() {
        ajaxMethod({
            url: '/api/coursetypes',
            success: function (data) {
                //data = JSON.parse(data)
                var $CourseColorTable = $('#CourseColor').html('');
                var $thead = $('<thead>\
                    <tr style="border:1px solid #ccc;border-botttom:none">\
                        <th>'+i18next.t('table.no')+'</th>\
                        <th>'+i18next.t('table.coursetype')+'</th>\
                        <th>'+i18next.t('table.color')+'</th>\
                        <th>'+i18next.t('table.remark')+'</th>\
                        <th>'+i18next.t('table.handle')+'</th>\
                    </tr>\
               </thead>').appendTo($CourseColorTable);
                var $tbody = $('<tbody></tbody>').appendTo($CourseColorTable);
                $.each(data, function (index, item) {
                    var coursetypeuid = item.coursetypeuid;
                    var $enroll = $('<tr class="enroll" data-uid="' + coursetypeuid + '"  ></tr>').appendTo($tbody);
                    $enroll.append('<td>1</td>')
                    $enroll.append('<td>' + item.coursetypename + '</td>')
                    var $color = $('<td> <input  type="text"  value="' + item.colorvalue + '"  style="width:60px;border:none;border:1px solid #fff;background:#fff;color:transparent;"/></td>').appendTo($enroll)
                    $enroll.append('<td>'+item.remark+'</td>')
                    var $Edittd = $("<td></td>").appendTo($enroll);
                    var $Edit = $('<a class="btn btn-primary btn-sm btn-inverse">'+i18next.t('table.compile')+'</a>').appendTo($Edittd);
                    var $delete = $('<a class="btn btn-danger btn-sm btn-inverse" style="margin-left:15px;">'+i18next.t('table.delete')+'</a>').appendTo($Edittd);
                    $delete.on('click', function () {
                      var popUpBox = new PopUpBox({
                          type: 'dom',
                          style: 'default contentScroll',
                          submitBtnType: 'confirm',
                          titleText: i18next.t('popupbox.warmprompt'),//温馨提示
                          show: true,
                          content: '<div style="margin-left:27px;margin-top:34px;">'+i18next.t('popupbox.del')+'</div>',//您确定要删除吗
                          callback: {
                            yes: function (e) {
                              $.ajax({
                                  url: "/api/coursetype/" + coursetypeuid,
                                  type: "delete",
                                  success: function () {
                                      getCouerse();

                                  }
                              })
                            }
                          },
                          position: {
                              width: 550,
                              height: 350
                          }
                      })
                    })
                    var hxcolor = $color.find('input').val();
                    $color.find('input').css('background', hxcolor);
                    $Edit.on("click", function () {
                      loadEnrollData(coursetypeuid);

                      $EditAdd.find('input[name="mycolor"]').iColor({ 'x': 10, 'y': -50 });
                         $EditAdd.find('#lineColor').iColor(function (hx) {
                           // console.log('自定义回调：去掉内容');
                           this.val('').css('background', '#' + hx);
                       });
                      var colorvalue1="";
                      var colorvalue2 = "";
                      colorvalue1 = $EditAdd.find('#lineColor').attr('hx')
                      colorvalue2 = $EditAdd.find('#witercolor').val();
                      console.log(colorvalue1)
                      console.log(colorvalue2)
                      var popUpBox = new PopUpBox({
                        type: 'dom',
                        style: 'default contentScroll',
                        submitBtnType: 'confirm',
                        titleText: i18next.t('popupbox.editcontent'),//请编辑内容
                        show: true,
                        content: $EditAdd,
                        callback: {
                          yes: function (e) {
                          bindSubmit();
                            // var colorvaluenew1 = $EditAdd.find('#lineColor').attr('hx');  //背景
                            // var colorvaluenew2 = $EditAdd.find('#witercolor').val();      //色值
                            //   if(colorvalue1==colorvaluenew1||colorvalue2!=colorvaluenew2){
                            //     //flage=true;
                            //     bindSubmit(true);
                            //   }else if(colorvalue1!=colorvaluenew1||colorvalue2==colorvaluenew2){
                            //     //flage=false;
                            //     bindSubmit(false);
                            //   }else{
                            //     //flage=false;
                            //     bindSubmit(false);
                            // }
                          }
                        },
                        position: {
                          width: 550,
                          height: 350
                        }
                      })
                    })
                 })
                var len = $('table tr').length;
                for (var i = 1; i < len; i++) {
                  $('table tr:eq(' + i + ') td:first').text(i);
                }
              }
          })
    }
    getCouerse();

    //点击添加学生
    $("#CourserColor").on('click', function () {
      if($EditAdd.find('input').val()!=""){
         $EditAdd.find('input').val("");
       }
      $EditAdd.find('input[name="mycolor"]').iColor({ 'x': 10, 'y': -50 });
      $EditAdd.find('#lineColor').iColor(function (hx) {
      this.val('').css('background', '#' + hx);
      });
      var popUpBox = new PopUpBox({
          type: 'dom',
          style: 'default contentScroll',
          submitBtnType: 'confirm',
          titleText:i18next.t('popupbox.pleasecontent'),//请填写内容
          show: true,
          content: $EditAdd,
          callback: {
            yes: function (e) {
                if ($EditAdd.find("input[type='text']").val() != "") {
                  var coursetypeuid = $EditAdd.find('[name=coursetypeuid]').val()
                  var coursetypename = $EditAdd.find('[name=coursetypename]').val()
                  var remark = $EditAdd.find('[name=remark]').val()
                  var colorvalue1 = $EditAdd.find('[name=mycolor]').attr('hx')
                  var colorvalue = $EditAdd.find('[name=color]').val()
                  var colorvalue2 = "";
                  if(colorvalue1!="fff"){
                    colorvalue2 = '#' + colorvalue1;
                  }else{
                    colorvalue2 =colorvalue;
                  }
                  var Studentroll = {
                      coursetypename: coursetypename,
                      colorvalue: colorvalue2,
                      remark:remark
                  };
                  ajaxMethod({
                      url: '/api/coursetype',
                      type: 'post',
                      data: Studentroll,
                      success: function (data) {
                          window.location.reload();
                      }
                  });
                } else {
                    return false;
                }
            }
          },
          position: {
            width: 550,
            height: 350
          }
      })

    })
   function loadEnrollData(coursetypeuid, callback) {
        ajaxMethod({
            url: '/api/coursetype/' + coursetypeuid,
            dataType: 'json',
            success: function (data) {
                console.log(data);
                //data = JSON.parse(data)
                $EditAdd.find('[name=coursetypeuid]').val(data.coursetypeuid);
                $EditAdd.find('[name=coursetypename]').val(data.coursetypename);
                $EditAdd.find('[name=remark]').val(data.remark);
                $EditAdd.find('[name=mycolor]').attr("hx", data.colorvalue);
                $EditAdd.find('[name=mycolor]').css("background",data.colorvalue);
                $EditAdd.find('[name=color]').val(data.colorvalue);
               (typeof callback == 'function') && callback()
            }
        })
    }

    function bindSubmit(data) {
      if (FormCheck.childrenInputCheck($EditAdd.find('#J_enroll_wrap'))) {
            var coursetypeuid = $EditAdd.find('[name=coursetypeuid]').val()
            var coursetypename = $EditAdd.find('[name=coursetypename]').val()
            var remark = $EditAdd.find('[name=remark]').val()
            var colorvalue1 = $EditAdd.find('[name=mycolor]').attr('hx')
            var colorvalue = $EditAdd.find('[name=color]').val()
            var colorvalue2 = '#' + colorvalue1;
            var newcolor=""
            $EditAdd.find('[name=mycolor]').on('blur',function(){
              newcolor=$(this).attr('hx');
            })
            // if(colorvalue1.substr(0,1)=="#"){
            //     colorvalue2 = colorvalue1;
            // }else{
            //    colorvalue2 = '#' + colorvalue1;
            // }
            var Studentroll = {
                coursetypeuid: coursetypeuid,
                coursetypename: coursetypename,
                colorvalue: colorvalue2,
                remark:remark
              }
            var arr = {
                coursetypeuid: coursetypeuid,
                coursetypename: coursetypename,
                colorvalue: colorvalue,
                remark:remark
              }
            if (coursetypeuid!=undefined) {
              ajaxMethod({
                  url: '/api/coursetype/'+coursetypeuid,
                  type: 'put',
                  data: Studentroll,
                  success: function (data,status) {
                    if(status.code!=0){
                      _Tips(status.message)
                    }else{
                      window.location.reload();
                    }
                  }
              });
              //  if(flage=false){
              //   ajaxMethod({
              //       url: '/api/coursetype/'+coursetypeuid,
              //       type: 'put',
              //       data: Studentroll,
              //       success: function (data,status) {
              //         if(status.code!=0){
              //           _Tips(status.message)
              //         }else{
              //           window.location.reload();
              //         }
              //       }
              //   });
              // }else{
              //   ajaxMethod({
              //       url: '/api/coursetype/'+coursetypeuid,
              //       type: 'put',
              //       data: arr,
              //       success: function (data,status) {
              //         if(status.code!=0){
              //           _Tips(status.message)
              //         }else{
              //           window.location.reload();
              //         }
              //       }
              //   });
              // }

            }
         }
    }

})
